package cate.game.role.res.call.nature;

import cate.common.table.call.row.CallWeightRow;
import cate.common.table.d.GD;
import cate.common.table.d.GDHeroCall;
import cate.common.table.d.GDObj;
import cate.common.table.item.hero.StarGroupList;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.common.util.GameResult;
import cate.common.util.random.RWList;
import cate.game.res.Option;
import cate.game.role.Role;
import cate.game.role.bag.hero.Hero;
import cate.game.role.res.call.CallBasePO;
import cate.game.role.res.call.CallCommonBaseHandler;
import cate.game.role.res.call.base.CallProcessData;
import cate.game.role.res.call.base.CallResult;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

public class CallNatureExchangeHandler extends CallCommonBaseHandler {


  @Override
  public boolean callPreCheck(Role role, CallProcessData processData, CallBasePO basePO) {
    if (!super.callPreCheck(role, processData, basePO)) {
      return false;
    }
    GameResult<Integer> r = checkHeroGroup(role, processData);
    if (!r.ok()) {
      role.getGame().notice.message(role, r.message);
      return false;
    }
    return true;
  }

  private GameResult<Integer> checkHeroGroup(Role role, CallProcessData processData) {
    GameResult<Integer> r = new GameResult<>();

    if (processData.cs == null || CollectionUtils.isEmpty(processData.cs.options)) {
      return r.fail("未选择英雄");
    }
    Hero hero = null;
    for (Option option : processData.cs.options) {
      if (option.type != GDObj.Type.HERO) {
        continue;
      }
      for (String uid : option.uidList) {
        if (hero != null) {
          return r.fail("选择了多个英雄");
        }
        hero = role.getBag().hero.getItem(uid);
      }
    }
    if (hero.lock.islocked()) {
      role.getGame().notice.message(role, "英雄已上锁");
      return r.fail("英雄已上锁");
    }

    HeroBaseRow heroBaseRow = hero.getTpl(role.getGame());
    if (heroBaseRow == null) {
      role.getGame().notice.message(role, "英雄不存在");
      return r.fail("英雄不存在");
    }

    if (heroBaseRow.star != 5) {
      role.getGame().notice.message(role, "无法置换该英雄");
      return r.fail("无法置换该英雄");
    }

    boolean heroExist = false;
    for (StarGroupList.Item rwItem : processData.base.config.list) {
      CallWeightRow row = role.getGame().table.call.weight.getRowByHeroId(rwItem.groupId, hero.tid);
      if (row == null) {
        rwItem.weight = GD.INT_0;
        continue;
      }
      heroExist = true;
      rwItem.weight = GDHeroCall.WEIGHT_BASE;
    }

    if (!heroExist) {
      return r.fail("选中英雄无法进行置换");
    }
    hero.lock.addReplaceLock(role);
    return r.success();
  }

  @Override
  public void singleCallExe(Role role, CallProcessData processData, CallBasePO basePO) {
    processData.base.config.setup();
    StarGroupList.Item group = processData.base.config.get();
    if (group == null) {
      role.getGame().notice.message(role, "配置错误");
      return;
    }
    CallResult item = null;
    //普通模式
    Map<Integer, CallWeightRow> map = role.getGame().table.call.weight.groupMap.get(group.groupId);
    if (map == null) {
      role.getGame().notice.message(role, "配置错误");
      return;
    }
    ArrayList<CallWeightRow> list = new ArrayList<>(map.values());
    Hero hero = onGetHero(role, processData);
    if(Objects.isNull(hero)){
      return;
    }
    list.removeIf(row -> row.heroId == hero.tid);

    CallWeightRow row = basePO.tool.doGeneralCall(role, list);
    if (row == null) {
      return;
    }
    item = new CallResult(processData.consumedTimes + 1, row.id);

    processData.result.add(item);
  }

  private Hero onGetHero(Role role, CallProcessData processData) {
    Hero hero = null;
    for (Option option : processData.cs.options) {
      if (option.type != GDObj.Type.HERO) {
        continue;
      }
      for (String uid : option.uidList) {
          if (hero != null) {
            break;
          }
          hero = role.getBag().hero.getItem(uid);
      }
    }
    return hero;
  }
}
